package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.VisitTemplate;
import com.yixin.hospital.entity.dto.req.visitTemplate.VisitTemplateAddReqDTO;
import com.yixin.hospital.entity.dto.req.visitTemplate.VisitTemplateEditReqDTO;
import com.yixin.hospital.entity.dto.req.visitTemplate.VisitTemplateEditTemplateReqDTO;
import com.yixin.hospital.service.VisitTemplateService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/visit-template")
@Api(value = "就诊模板管理", tags = "就诊模板管理")
public class VisitTemplateController {

    private VisitTemplateService visitTemplateService;

    @GetMapping("/list")
    @ApiOperation(value = "就诊模板列表（树状）")
    public ApiResult list(String templateCode) {
        return visitTemplateService.treeList(templateCode);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增就诊管理类别")
    public ApiResult add(@Valid @RequestBody VisitTemplateAddReqDTO visitTemplateAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitTemplateService.add(visitTemplateAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改就诊管理类别")
    public ApiResult edit(@Valid @RequestBody VisitTemplateEditReqDTO visitTemplateEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitTemplateService.edit(visitTemplateEditReqDTO, userName);
    }

    @PutMapping("/edit-templateName")
    @ApiOperation(value = "修改模板名称")
    public ApiResult editTemplateName(@Valid @RequestBody VisitTemplateEditTemplateReqDTO visitTemplateEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return visitTemplateService.editTemplateName(visitTemplateEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除就诊管理类别")
    public ApiResult remove(@PathVariable int id) {
        visitTemplateService.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("/template-list")
    @ApiOperation(value = "就诊模板列表")
    public ApiResult templateList() {
        List<VisitTemplate> list = visitTemplateService.list(new LambdaUpdateWrapper<VisitTemplate>().groupBy(VisitTemplate::getTemplateCode));
        return ApiResult.success(list);
    }

}
